Skip to content

refactor: consolidate reference tracking with Tracker in deployer#3687

Open
Ankitsinghsisodya wants to merge 2 commits intoknative:mainfrom
Ankitsinghsisodya:refactor/issue-3678-consolidate-reference-tracking
Open

refactor: consolidate reference tracking with Tracker in deployer#3687
Ankitsinghsisodya wants to merge 2 commits intoknative:mainfrom
Ankitsinghsisodya:refactor/issue-3678-consolidate-reference-tracking

Conversation

@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor

@Ankitsinghsisodya Ankitsinghsisodya commented May 9, 2026

What

Fixes #3678
Fixes the three code smells called out in #3678 by introducing a Tracker
struct (shape b from the issue) that owns the three reference sets and
accumulates them via receiver methods.

Changes

pkg/k8s/deployer.go

  • Add References struct holding Secrets, ConfigMaps, PVCs as
    sets.Set[string] values (not pointers — maps are already reference types).
  • Add Tracker struct embedding References and NewTracker() constructor
    that initialises all three sets.
  • Convert ProcessEnvs, ProcessVolumes, createEnvFromSource,
    createEnvVarSource from free functions with *sets.Set[string]
    out-parameters to *Tracker methods that write directly into the receiver.
  • Update generateDeployment to accept *Tracker instead of three pointer
    parameters.
  • Update CheckResourcesArePresent to accept References by value instead of
    three *sets.Set[string] parameters.
  • Update both call sites in Deploy() (create and update paths).

pkg/knative/deployer.go

  • Update generateNewService signature to accept *k8s.Tracker.
  • Update both call sites in Deploy() (create and update paths).
  • Remove now-unused sets import.

pkg/k8s/deployer_test.go / pkg/knative/deployer_test.go

  • Replace three-variable sets.New[string]() + pointer passing with
    NewTracker() in all affected tests.
  • Update regression test assertions to use tracker.Secrets /
    tracker.ConfigMaps.
  • Remove unused sets imports.

Why shape (b) — Tracker receiver

  • Structurally prevents the fix: apply resource validation to first-deploy path in k8s and keda deployers #3671 bug class. With out-parameters a caller
    can silently create a disconnected set and pass it alongside the one that
    gets validated; with a single Tracker receiver there is nothing to
    disconnect.
  • Eliminates the pointer-to-map double-indirection. sets.Set[string] is
    map[string]Empty; passing it by value inside References already lets
    callees mutate the same backing store without the extra *.
  • One argument instead of three at every call site.

Behavior

No behavior changes. This is a pure shape refactor.

Test plan

  • go test ./pkg/k8s/ ./pkg/knative/ — both pass
  • make check — 0 lint issues
  • make test — all pre-existing unit tests pass

…ructure

- Replaced individual sets for referenced secrets, config maps, and PVCs with a Tracker that encapsulates these references.
- Updated the generateDeployment and CheckResourcesArePresent functions to utilize the Tracker, ensuring consistent resource validation.
- Modified related tests to verify that the Tracker correctly accumulates resource references during deployment generation.

This change enhances the clarity and maintainability of the code by consolidating resource tracking logic.
Copilot AI review requested due to automatic review settings May 9, 2026 14:09
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 9, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Ankitsinghsisodya
Once this PR has been reviewed and has the lgtm label, please assign matzew for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 9, 2026 14:10
@knative-prow knative-prow Bot added size/L 🤖 PR changes 100-499 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 9, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 9, 2026

Hi @Ankitsinghsisodya. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors reference tracking for Kubernetes resource validation by introducing a k8s.Tracker/k8s.References shape, and threads it through the k8s and knative deployers so referenced Secrets/ConfigMaps/PVCs are accumulated consistently during manifest generation and then validated.

Changes:

  • Added References and Tracker (with NewTracker()) to consolidate the three reference sets into a single accumulator.
  • Converted env/volume processing helpers to *Tracker receiver methods and updated deploy paths to pass a tracker through generation and validation.
  • Updated knative and k8s unit tests to use NewTracker() and to assert against tracker.Secrets / tracker.ConfigMaps.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
pkg/k8s/deployer.go Introduces References/Tracker, refactors env/volume processing and resource validation APIs to use the tracker.
pkg/knative/deployer.go Threads *k8s.Tracker through service generation and resource validation on create/update paths; removes unused sets import.
pkg/k8s/deployer_test.go Updates deployment-generation tests to pass a Tracker instead of three sets.
pkg/knative/deployer_test.go Updates regression test to use NewTracker() and assert tracked references via tracker fields.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/k8s/deployer.go
Comment thread pkg/k8s/deployer.go
Comment thread pkg/k8s/deployer.go Outdated
Comment thread pkg/knative/deployer.go Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented May 9, 2026

Codecov Report

❌ Patch coverage is 54.25532% with 43 lines in your changes missing coverage. Please review.
✅ Project coverage is 56.11%. Comparing base (0209813) to head (2afde2e).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/k8s/deployer.go 48.78% 37 Missing and 5 partials ⚠️
pkg/knative/deployer.go 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3687      +/-   ##
==========================================
- Coverage   56.90%   56.11%   -0.79%     
==========================================
  Files         181      181              
  Lines       20933    20974      +41     
==========================================
- Hits        11912    11770     -142     
- Misses       7811     8033     +222     
+ Partials     1210     1171      -39     
Flag Coverage Δ
e2e 36.08% <52.12%> (-0.08%) ⬇️
e2e go ?
e2e node ?
e2e python ?
e2e quarkus ?
e2e rust ?
e2e springboot ?
e2e typescript ?
e2e-config-ci 17.84% <0.00%> (-0.04%) ⬇️
integration 17.43% <55.42%> (+<0.01%) ⬆️
unit macos-14 44.98% <21.68%> (-0.01%) ⬇️
unit macos-latest 44.98% <21.68%> (-0.01%) ⬇️
unit ubuntu-24.04-arm 45.13% <20.21%> (-0.03%) ⬇️
unit ubuntu-latest 45.83% <21.68%> (-0.02%) ⬇️
unit windows-latest 44.96% <21.68%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…ssVolumes functions

- Introduced ensureInit method in Tracker to initialize nil sets for Secrets, ConfigMaps, and PVCs, preventing panics on nil-map Insert.
- Updated ProcessEnvs and ProcessVolumes functions to call the new Tracker methods, ensuring backward compatibility while encouraging the use of the Tracker directly.
- Improved error messages in knative deployer for resource validation failures, enhancing clarity in error reporting.

This change improves the robustness of resource tracking and prepares for future refactoring by phasing out deprecated functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test 🤖 Needs an org member to approve testing size/L 🤖 PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: consolidate reference tracking in pkg/k8s/deployer.go

2 participants